﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Bloks
{
    public class Levels
    {
        private List<Deathbox> screenpos = new List<Deathbox>();
        private Vector2 deathpos;
        private string[,] leveldesign;
        private float _deltax;

        public void GetLevel(Texture2D leveldata)
        {
            Color[] data1D = new Color[leveldata.Width * leveldata.Height];
            leveldata.GetData<Color>(data1D);

            leveldesign = new string[leveldata.Width, leveldata.Height];
            int j = 0, k = 0;
            for (int i = 0; i < data1D.Length; i++)
            {
                //This if statement is only called when we reach the "edge" of the texture
                //It insures that we move down the 2nd dimension of the 2-D array
                if (i % leveldata.Width == 0 && i != 0)
                {
                    j++;
                    k = 0;
                }
                leveldesign[k, j] = data1D[i].R.ToString() + data1D[i].G.ToString() + data1D[i].B.ToString();
                k++;
            }
        }

        public void Load(Texture2D texture2)
        {
            for (int i = 0; i < leveldesign.GetLength(0); i++)
                for (int j = 0; j < leveldesign.GetLength(1); j++)
                {
                    if (leveldesign[i, j] == "25500")
                    {
                        deathpos = new Vector2(i * 16, j * 16);
                        screenpos.Add(new Deathbox(deathpos, texture2));
                    }
                }
        }
        public bool Collision(Rectangle player)
        {
            foreach (Deathbox aDeathbox in screenpos)
            {
                if (player.Intersects(new Rectangle((int)aDeathbox.Screenpos.X, (int)aDeathbox.Screenpos.Y, aDeathbox.DeathboxTex.Width, aDeathbox.DeathboxTex.Height)))
                {
                    //aDeathbox.Screenpos = new Vector2(aDeathbox.Screenpos.X, aDeathbox.Screenpos.Y - 10);
                    return true;
                }
            }
            return false;
        }

        public void Update(float deltaX)
        {
            _deltax = deltaX;
            foreach (Deathbox aDeathbox in screenpos)
            {
                aDeathbox.Screenpos = new Vector2(aDeathbox.Screenpos.X - _deltax, aDeathbox.Screenpos.Y);
            }
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            foreach (Deathbox aDeathbox in screenpos)
            {
                spriteBatch.Draw(aDeathbox.DeathboxTex, aDeathbox.Screenpos, Color.White);
            }
        }

        public float Ingenblokunder(Vector2 screenposplayer)
        {
            foreach (Deathbox aDeathbox in screenpos)
            {
                float hvorlangtfraplayer = aDeathbox.Screenpos.X - 100;
                 if (hvorlangtfraplayer < 50 && hvorlangtfraplayer > 0)
                {
                    if (aDeathbox.Screenpos.Y - screenposplayer.Y > 8 && aDeathbox.Screenpos.Y - screenposplayer.Y < 10)
                    {
                        return aDeathbox.Screenpos.Y - 16;
                    }
                    
                }
            }
            return 409;
        }
    }
}